<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Routing/Geocoding examples</title> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript" language="javascript" src="js/OpenError.js"></script> 
<script type="text/javascript" language="javascript" src="js/data_params.js"></script> 
<script type="text/javascript" language="javascript" src="js/DispObjects.js"></script> 
<script type="text/javascript"> 
  var directionDisplay;
  var directionsService = new google.maps.DirectionsService();
  var geocoder = new google.maps.Geocoder();
  var georesponse;
  
  var map;
  var oe;
  var test = {distance:[],duration:[]};
  
 
  function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var myOptions = {
      zoom: 15,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: new google.maps.LatLng(52.635,-1.13055)
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
	
	// create a new openerror object for introducing controlled error
	oe = new OpenError();
	oe.setDataSet(ptsLei2);  // var created on dataset.js file
	oe.setStandardDeviation(90); //1609.344);
	oe.setDebug( false );
	oe.setCorrelated( false );
	
	// create a new display object for organizing map data
	disp = new DispObject("map", "g3");
	//calcRoute();
  }
var resp;
 
  function geocode_response(response, status)
  {
	if(status == google.maps.GeocoderStatus.OK)
	{
		
		if( response.length > 1)
		{
			// added other locations as options
		}
		else
		{
			
		}
	}
  }
  function geocode()
  {
	var start = document.getElementById('start').value;
	var request = {
		address:start
	};
	geocoder.geocode(request, function(response, status) {
		if(status == google.maps.GeocoderStatus.OK)
		{
			if( response.length > 0)
			{
				var lat = response[0].geometry.location.lat();
				var lng = response[0].geometry.location.lng();
				//var coord = lat+","+lng;
				ptsLei2.coordinates[0]=[lat, lng]; 
				document.getElementById('geocoded_start').value=lat+", "+lng;
				document.getElementById('start_zoom_link').onclick=(function() {map.panTo( new google.maps.LatLng( lat, lng) );});
			}
			else
			{
				
			}
		}
	});
	
	var end = document.getElementById('end').value;
	var request2 = {
		address:end
	};
	geocoder.geocode(request2, function(response, status) {
		if(status == google.maps.GeocoderStatus.OK)
		{
			if( response.length > 0)
			{
				var lat = response[0].geometry.location.lat();
				var lng = response[0].geometry.location.lng();
				//var coord = lat+","+lng;
				ptsLei2.coordinates[1]=[lat, lng]; 
				document.getElementById('geocoded_end').value=lat+", "+lng;
				document.getElementById('end_zoom_link').onclick=(function() {map.panTo( new google.maps.LatLng( lat, lng) );});
			}
			else
			{
				
			}
		}
	});
   // Position the map to cover both start and end extents. 
   
  }
  function calcRouteTen()
  {
	for(var i=0;i<10;i++)
		calcRoute();
  }
  
  function calcRoute() {
  
   oe.setStandardDeviation(parseFloat(document.getElementById('sigma').value));
	var points = disp.addPoints();
	disp.hidePoints();
	//begin = "london, uk"; //document.getElementById("start");
	//end = "leicester, uk"; //document.getElementById("end");
    begin = new google.maps.LatLng(points[0].lat, points[0].lon);
    end = new google.maps.LatLng(points[1].lat, points[1].lon);
		//var selectedMode = document.getElementById("mode").value;
    var request = {
        origin: begin,
        destination: end,
        // Note that Javascript allows us to access the constant
        // using square brackets and a string value as its
        // "property."
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        //directionsDisplay.setDirections(response);
		resp = response;
		test.distance.push(resp.routes[0].legs[0].distance.value);
		test.duration.push(resp.routes[0].legs[0].duration.value);
		
 
			polylineOptions = {
					map: map,
					strokeColor: "#FF0000",
					strokeOpacity: 0.3,
					strokeWeight: 5,
					path: response.routes[0].overview_path
			}
			new google.maps.Polyline(polylineOptions);
 
      }
    });
  }
</script> 
</head> 
 
<FONT FACE="Arial, Helvetica, Geneva"> 
 
<body style="margin:0px; padding:0px;" onload="initialize()"> 
<h2>Propagating Positional Uncertainty to Routing Applications</h2> 
<p>This example demonstrates how OpenError can be used to propagate
uncertainty to the Google Maps routing application. Positional error (90 m = 1 sd)
is simulated for origin and destination points in central Leicester (UK);
These locations are submitted to the routing API. The route is drawn
with a translucent red line. Press 'Route' to generate another point 
realization, calculate its route, and add it to the map. Small
differences in position can result in wildly different routes - this is 
error propagation!</p> 
<p>To route between your choice of spots, type in your own locations: 
Lat-Lon pairs, addresseses, or placenames. Change the magnitude of the
introduced error by modifying Sigma. Press 'Geocode' to identify the locations,
then press 'Route', or 'Route 10x', to observe variations in your trip.</p> 
<div> 
<b>Calculate a New Route: </b><br/> 
 
<table> 
	<tbody> 
		<tr> 
 
			<th id=pts></th> 
			<th id=locations>Locations</th> 
			<th id=displacement>Sigma (meters)</th> 
			<th id=coords>Geocoded Locations</th> 
		</tr> 
		<tr> 
		   <td headers= pts> 
 
				<strong>Start:</strong> 
			</td> 
			<td headers=locations> 
				<input type=text id=start value="221 Baker St london, uk"/> 
			</td> 
			<td headers=displacement> 
			   <input type=text id=sigma value="90" /> 
			</td> 
 
			<td headers=coords> 
				<input type=text id=geocoded_start disabled=disabled /><a href="#" id=start_zoom_link >Zoom to here</a> 
			</td> 
		</tr> 
		<tr> 
			<td> 
				<strong>End:</strong> 
			</td> 
 
			<td headers=locations> 
				<input type=text id=end value="Whitehall, London uk" /> 
			</td> 
			<td headers=displacement> 
			   <input type=text id=sigma2 disabled=disabled value="" /> 
			</td> 
			<td headers=coords> 
				<input type=text id=geocoded_end disabled=disabled /><a href="#" id=end_zoom_link >Zoom to here</a> 
 
			</td> 
		</tr> 
	</tbody> 
</table> 
			
<br/> 
<input type="button" value="Geocode" onclick="geocode();" /> 
<input type="button" value="Route" onclick="calcRoute();" /> 
<input type="button" value="Route 10x" onclick="calcRouteTen();" /> 
</div> 
<div id="map_canvas" style="width:100%; height:80%"></div> 
<p><a href='./index.html'><i>OpenError</i></a></p> 
</body> 
 
</html> 